﻿Imports TMySQL
Public Class frmTablePrivileges
    Private _PrivList As TMyTablePrivilegesList
    Private Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub

    Public Sub New(ByVal APrivs As TMyTablePrivilegesList)
        Me.New()
        _PrivList = APrivs
        InitFrm()
    End Sub
    Private Sub InitFrm()
        lstDatabase.Items.Clear()
        For i As Integer = 0 To _PrivList.Count - 1
            If Not lstDatabase.Items.Contains(_PrivList(i).DB) Then
                lstDatabase.Items.Add(_PrivList(i).DB)
            End If
        Next
    End Sub

    Private Sub lstDatabase_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDatabase.SelectedIndexChanged
        Cursor.Current = Cursors.WaitCursor
        lv.SuspendLayout()
        Try
            lv.Items.Clear()
            If lstDatabase.SelectedItems.Count <= 0 Then
                Return
            End If
            ShowTables(lstDatabase.SelectedItem)
        Finally
            lv.ResumeLayout()
            Cursor.Current = Cursors.Default
        End Try
    End Sub
    Private Sub ShowTables(ByVal ADatabaseName As String)
        lv.Items.Clear()
        For i As Integer = 0 To _PrivList.Count - 1
            If _PrivList(i).DB.Equals(ADatabaseName) Then
                Dim li As New ListViewItem
                li.Text = _PrivList(i).TableName
                li.SubItems.Add(_PrivList(i).Engine)
                li.SubItems.Add(Common.DateTimeToString(_PrivList(i).CreateTime))
                li.SubItems.Add(Common.DateTimeToString(_PrivList(i).UpdateTime))
                li.SubItems.Add(_PrivList(i).Grantor)
                li.SubItems.Add(_PrivList(i).TableComment)
                li.Tag = _PrivList(i)
                lv.Items.Add(li)
            End If
        Next
        For Each col As ColumnHeader In lv.Columns
            col.Width = -2
        Next
    End Sub

    Private Sub lv_ItemSelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles lv.ItemSelectionChanged
        Cursor.Current = Cursors.WaitCursor
        Try
            InitCheck()
            If Not e.IsSelected Then
                Return
            End If
            ShowPrivs(e.Item.Tag)
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub
    Private Sub ShowPrivs(ByVal APriv As TMyTablePrivileges)
        chkSelect.Checked = APriv.SelectPriv
        chkInsert.Checked = APriv.InsertPriv
        chkUpdate.Checked = APriv.UpdatePriv
        chkDelete.Checked = APriv.DeletePriv

        chkCreate.Checked = APriv.CreatePriv
        chkAlter.Checked = APriv.AlterPriv
        chkDrop.Checked = APriv.DropPriv
        chkIndex.Checked = APriv.IndexPriv

        chkGrant.Checked = APriv.GrantPriv
        chkReferences.Checked = APriv.ReferencesPriv
    End Sub
    Private Sub InitCheck()
        chkSelect.Checked = False
        chkInsert.Checked = False
        chkUpdate.Checked = False
        chkDelete.Checked = False

        chkCreate.Checked = False
        chkAlter.Checked = False
        chkDrop.Checked = False
        chkIndex.Checked = False

        chkGrant.Checked = False
        chkReferences.Checked = False
    End Sub
End Class